﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMSGSC.Common;
using DevExpress.XtraEditors;
using PMSGSC.DAL;
using PMSGSC.BLL;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using System.Globalization;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.Linq;
using DevExpress.Utils.OAuth.Provider;
using DevExpress.XtraRichEdit.Model;
using System.Diagnostics;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraTab;
using DevExpress.XtraRichEdit;
using DevExpress.XtraPrinting;

namespace PMSGSC.App
{
    public partial class frmReviewStatusOfEquipment : DevExpress.XtraEditors.XtraForm
    {
        frmMain objMain;
        public frmReviewStatusOfEquipment(frmMain main)
        {
            InitializeComponent();
            objMain = main;
            statusBindingSource.DataSource = Status_BLL.GetAll(frmMain.ship.Ship_ID);
            foreach (PMSGSC.DAL.Status s in statusBindingSource.Cast<PMSGSC.DAL.Status>().ToList())
            {
                comboBoxEdit1.Properties.Items.Add(s.NameStatus);
            }
        }

        private int GetIndexStatusName(string name)
        {
            foreach (PMSGSC.DAL.Status s in statusBindingSource.Cast<PMSGSC.DAL.Status>().ToList())
            {
                if (s.NameStatus == name)
                    return s.Status_ID;
            }
            return 0;
        }

        public List<Equipment> equipmentList = new List<Equipment>();
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            frmProcessBar pb = new frmProcessBar();
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                LoadSkinToForm();
                pb.ShowDialog();
            }
            ));
            thread.Start();

            equipmentList = Equipment_BLL.PrintStatusOfEquipment(txtFrom.Text, txtTo.Text,
              txtName.Text, GetIndexStatusName(comboBoxEdit1.Text), frmMain.ship.Ship_ID);

           

            if (equipmentList.Count != 0)
            {
                frmReportStatusOfComponent report = new frmReportStatusOfComponent(equipmentList);
                report.MdiParent = objMain;
                report.Show();

                if (pb.InvokeRequired)
                    pb.BeginInvoke(new Action(() => pb.Close()));

                this.Close();
            }
            else
            {
                if (pb.InvokeRequired)
                    pb.BeginInvoke(new Action(() => pb.Close()));

                XtraMessageBox.Show("Not result in display", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Office 2007 Blue");
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonEdit1_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            frmGridComponent components = new frmGridComponent();
            components.ShowDialog();

            if (components.equip != null)
                txtFrom.Text = components.equip.CodeEquipment;
        }

        private void txtTo_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            frmGridComponent components = new frmGridComponent();
            components.ShowDialog();

            if (components.equip != null)
                txtTo.Text = components.equip.CodeEquipment;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtFrom.Text = "";
            txtTo.Text = "";
            txtName.Text = "";
            comboBoxEdit1.SelectedIndex = -1;
        }
    }
}